# include <stdio.h>
# include <math.h>
# include "zx_fft.h"

double HRV_RMSSD(int *data,int len)
{
    int i;
    double sum=0;
    int temp,temp_pre;
    for(i=0;i<len-1;i++)
    {
        temp_pre=temp;
        temp=data[i+1]-data[i];
        if (i>0)
        {
            sum=sum+pow(temp-temp_pre,2);
        }
    }
    double RMSSD=sqrt(sum/(len-2))*4;
    return RMSSD;
}


double HRV_SDNN(int *data,int len)
{
    int i;
    double sum=0;
    int temp;
    double sum_mean=0;
    for(i=0;i<len-1;i++)
    {
        temp=data[i+1]-data[i];
        sum=sum+pow(temp,2);
        sum_mean=sum_mean+temp;
    }
    double SDNN=sqrt(sum/(len-1)-pow(sum_mean/(len-1),2))*4;
    return SDNN;
}

int respiration2(float *data,int num,int *pos,int len,int fs)
{
    int i,k=0;
    int TPM;
    if (len<3){
        printf("data length is too short");
    }
    for (i=1;i<len-1;i++)
    {
        if(data[pos[i]]>data[pos[i-1]]&&data[pos[i]]>data[pos[i+1]])
        {
            k++;
        }
    }
    TPM=k*fs*60/num;
    return TPM;
    
}




